package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.InverterDataSheet;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.InverterDataSheetService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/data/sheet")
public class InverterDataSheetController {

    @Resource(name = "inverterDataSheetService")
    private InverterDataSheetService inverterDataSheetService;

    @GetMapping
    public Result<Page<InverterDataSheet>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, String title) throws IframeException {
        Page<InverterDataSheet> page = Page.of(pageNo, pageNum);
        LambdaQueryWrapper<InverterDataSheet> lambdaQueryWrapper = new LambdaQueryWrapper<InverterDataSheet>();
        if(StringUtils.isNotEmpty(title)){
            lambdaQueryWrapper.like(InverterDataSheet::getTitle , title);
        }
        return Result.ok(inverterDataSheetService.page(page));
    }

    @GetMapping("/list")
    public Result<List<InverterDataSheet>> get() throws IframeException {
        LambdaQueryWrapper<InverterDataSheet> lambdaQueryWrapper = new LambdaQueryWrapper<InverterDataSheet>();
        lambdaQueryWrapper.eq(InverterDataSheet::getStatus , 0).orderByAsc(InverterDataSheet::getSort);
        return Result.ok(inverterDataSheetService.selectList(lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<InverterDataSheet> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(inverterDataSheetService.getById(id));
    }

    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody InverterDataSheet inverterDataSheet) throws IframeException {
        return Result.ok(inverterDataSheetService.save(inverterDataSheet));
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody InverterDataSheet inverterDataSheet) throws IframeException {
        return Result.ok(inverterDataSheetService.update(inverterDataSheet));
    }

    @DeleteMapping(produces = "application/json;charset=utf-8")
    public Result<Integer> delete(@RequestBody List<String> ids) throws IframeException {
        return Result.ok(inverterDataSheetService.delete(ids));
    }

    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(inverterDataSheetService.delete(id));
    }

    @GetMapping("/status")
    public Result<Boolean> status(@RequestParam("id") Integer id, @RequestParam("status") Integer status) throws IframeException {
        InverterDataSheet inverterDataSheet = new InverterDataSheet();
        inverterDataSheet.setId(id);
        inverterDataSheet.setStatus(status);
        return Result.ok(inverterDataSheetService.updateById(inverterDataSheet));
    }

}
